let express = require('express');
let router = express.Router();

const schoolService = require('../service/schoolService');

// 引入jwt验证方法的中间件
const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');
router.use(checkJWTMiddleWare);

/* 获取所有院校接口 */
router.get('/getAllSchool', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let result = await schoolService.getAllSchool(uid);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 收藏院校接口
 */
router.post('/collectSchool', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let collegeId = req.body.collegeId;
    if (!collegeId) {
        return res.status(400).json({ success: false, msg: '院校id为空' });
    }
    let result = await schoolService.collectSchool(uid, collegeId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 取消收藏院校接口
 */
router.post('/noCollectSchool', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let collegeId = req.body.collegeId;
    if (!collegeId) {
        return res.status(400).json({ success: false, msg: '院校id为空' });
    }
    let result = await schoolService.noCollectSchool(uid, collegeId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 懒加载获取收藏院校接口
 */
router.post('/getCollectSchool', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let lastId = req.body.lastId;
    let result = await schoolService.getCollectSchool(uid, lastId);
    res.status(result.success ? 200 : 400).json(result);
})

/*
 * 获取所有专业
 */
router.post('/getAllMajor', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let collegeId = req.body.collegeId;
    if (!collegeId) {
        return res.status(400).json({ success: false, msg: '院校id为空' });
    }
    let result = await schoolService.getAllMajor(collegeId, uid);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 懒加载获取某个院校资讯信息
 */
router.post('/getAllzixun', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { collegeId, lastId } = req.body;
    if (!collegeId) {
        return res.status(400).json({ success: false, msg: '院校id为空' });
    }
    let result = await schoolService.getAllzixun(uid, collegeId, lastId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 懒加载推荐资讯信息
 */
router.post('/getRecommendzixun', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { lastId } = req.body;
    let result = await schoolService.getRecommendzixun(uid, lastId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 获取院校详细信息
 */
router.post('/getSchoolInfo', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { collegeId } = req.body;
    if (!collegeId) {
        return res.status(400).json({ success: false, msg: '院校id为空' });
    }
    let result = await schoolService.getSchoolInfo(uid, collegeId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 获取资讯内容
 */
router.post('/getzixunContent', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { zixunId } = req.body;
    if (!zixunId) {
        return res.status(400).json({ success: false, msg: '资讯id为空' });
    }
    let result = await schoolService.getzixunContent(zixunId, uid);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 分页获取所有院校接口
 */
router.get('/loadAllSchool', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let page = parseInt(req.query.page || 1);
    let result = await schoolService.loadAllSchool(uid, page);
    res.status(result.success ? 200 : 400).json(result);
});

module.exports = router;